package com.chance.cc.crawler.development.command.job.domain.dxy.trace;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.domain.dxy.DXYCommonScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lt
 * @version 1.0
 * @date 2021-02-05 18:04:16
 * @email okprog@sina.com
 */
public class DXYForumTraceCrawlerSchedulerJob extends DXYCommonScript {
    private static final String crawler_level = "trace";


    public static void main(String[] args) {

//        publishDXYCrawlerScheduleJobInfo();
        System.out.println(dxyScheduler().generateCrawlerTriggerKey());
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishDXYCrawlerScheduleJobInfo(){

        CrawlerJob dxyScheduler = dxyScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(dxyScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("丁香园论坛关键词回溯七天回复采集任务");
        crawlerScheduleJob.setCrawlerKey(dxyScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return dxyScheduler;
    }

    public static CrawlerJob dxyScheduler(){

        //文章采集
        CrawlerRequestRecord forumCrawler = doSearchBBSCrawler();
        String site = forumCrawler.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site, crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue")))
                .kafkaResultPipeline(null,kafkaTopicForTraceJob,null)
                .fileResultPipeline(null,"/data/chance_crawler_test/data/dxy/dxy_forum_trace.log",false)
                .crawlerJobThreadNumber(10)
                .requestRecord(forumCrawler)
                .supportRecord(keywordCrawlerRecord)
                .build();
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doSearchBBSCrawler(){
        String site = "forum";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain,CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey("https://portal.dxy.cn/")
                .httpUrl("https://portal.dxy.cn/")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .proxy(proxy)
                .needParsed(false)
                .needWashed(false)
                .build();

        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(StringUtils.joinWith("-","forum","trace"));
        return requestRecord;
    }
}
